import hashlib


def calculate_file_hash(file_path, algorithm='md5'):
    try:
        hash_object = None
        if algorithm == 'md5':
            hash_object = hashlib.md5()
        elif algorithm == 'sha1':
            hash_object = hashlib.sha1()
        elif algorithm == 'sha256':
            hash_object = hashlib.sha256()
        else:
            raise ValueError("不支持的哈希算法，仅支持 'md5', 'sha1', 'sha256'。")

        with open(file_path, 'rb') as file:
            for chunk in iter(lambda: file.read(4096), b""):
                hash_object.update(chunk)

        return hash_object.hexdigest()
    except FileNotFoundError:
        print(f"错误: 文件 {file_path} 未找到。")
    except Exception as e:
        print(f"发生未知错误: {e}")


if __name__ == "__main__":
    file_path = '../test/test.txt'  # 替换为实际的文件路径
    md5_hash = calculate_file_hash(file_path, 'md5')
    sha1_hash = calculate_file_hash(file_path, 'sha1')
    sha256_hash = calculate_file_hash(file_path, 'sha256')

    print(f"MD5 哈希值: {md5_hash}")
    print(f"SHA - 1 哈希值: {sha1_hash}")
    print(f"SHA - 256 哈希值: {sha256_hash}")
